#include <gtk/gtk.h>
#include<stdio.h>
#include<string.h>
#include "save.h"
#include "newinfo.h"
#include "file.h"
/*
 * when you select another group,we must change,and show the informations which in the
 * group you choose.so we should save your modification of current data file if current data 
 * file modification flags is TRUE(it means global_file->cur_file->modify=TRUE).
 */
gboolean save_tmp_file(struct newinfo_struct *newinfo)
{
	FILE *fp_tmp;
	gchar tmp[80];
	gint i,j;
	gchar *ptext=NULL;
	struct file_ctrl *global_file=newinfo->global_file;
	struct clist_struct *clist=newinfo->clist;
	#ifdef _DEBUG_
		printf("save_tmp_file:------\n");
	#endif
	g_snprintf(tmp,80,"%s/%s",global_file->tmp,global_file->cur_file->name);
	if((fp_tmp=fopen(tmp,"w"))==NULL) {
		perror("fopen");
		return FALSE;
	}
	fseek(fp_tmp,0,SEEK_SET);
	printf("open:%s\n",tmp);
	g_snprintf(tmp,80,"info_nums= %d\ncol_nums= %d\ncolumns= %s\n",
				global_file->cur_file->info_nums,global_file->cur_file->col_nums,
				global_file->cur_file->columns);
	
	if(fprintf(fp_tmp,"%s",tmp)!=strlen(tmp)) {
		perror("fprintf:");
		return FALSE;
	}
		
	#ifdef _DEBUG_
		printf("read from clist_show:----\n");
	#endif
	for(i=0;i<global_file->cur_file->info_nums;i++) {
		for(j=0;j<global_file->cur_file->col_nums;j++) {
			gtk_clist_get_text(GTK_CLIST(clist->clist_show),i,j,&ptext);
			
			#ifdef _DEBUG_
				printf("%s ",ptext);
				
			#endif
			strcpy(tmp,ptext);
			if(fprintf(fp_tmp,"%s ",tmp)!=(strlen(tmp)+1)) {
				perror("fprintf:");
				fclose(fp_tmp);
				return FALSE;
			}
		}
		#ifdef _DEBUG_
			printf("\n");
		#endif
		if(fprintf(fp_tmp,"\n")!=1) {
			perror("fprintf:");
			fclose(fp_tmp);
			return FALSE;
		}
	}
	fclose(fp_tmp);	
	return TRUE;
}

gboolean save_all_file(struct file_ctrl *global_file)
{
	gchar tmp[80],line[256];
	FILE *fp_data,*fp_tmp;
	gint i,j,len;
	struct file_list *files;
	#ifdef _DEBUG_
		printf("save_all_file:----\n");
	#endif
	
	save_config_file(global_file);

	/*save each data file*/
	for(files=global_file->files;files->next != NULL;) {
		if(files->modify) {
			#ifdef _DEBUG_
				printf("save data file: %s\n",files->name);
			#endif
			g_snprintf(tmp,80,"%s/%s",global_file->tmp,files->name);
			if((fp_tmp=fopen(tmp,"r"))==NULL) {
				perror("fopen");
				return FALSE;
			}
			g_snprintf(tmp,80,"%s/%s",global_file->data,files->name);
			if((fp_data=fopen(tmp,"w"))==NULL) {
				perror("fopen");
				return FALSE;
			}
			for(j=0;j<3+files->info_nums&&!feof(fp_tmp);j++) {
				if(fgets(line,256,fp_tmp)==NULL) {
					perror("fgets");
					return FALSE;
				}
				#ifdef _DEBUG_
					printf("read:%s",line);
				#endif

				if(fprintf(fp_data,"%s",line)!=strlen(line)) {
					perror("fprintf:");
					return FALSE;
				}
			}
			fclose(fp_tmp);
			fclose(fp_data);
			files->modify=FALSE;
		}
		files=files->next;
	}
	return TRUE;
}

gboolean save_config_file(struct file_ctrl *global_file)
{
	gchar tmp[80];
	FILE *fp_config;
	gint i,j,len;
	struct file_list *files;
	#ifdef _DEBUG_
		printf("save_config_file:----\n");
	#endif
	if(global_file->modify) {
		/*there has some group benn added or renamed,so we must save this change*/
		g_snprintf(tmp,80,"%s/config",global_file->config);
		if((fp_config=fopen(tmp,"w"))==NULL) {
			perror("fopen");
			return FALSE;
		}
		g_snprintf(tmp,80,"language= %d\ngroup_nums= %d\ndefault= %s\ngroups= ",
			global_file->language,global_file->group_nums,global_file->current);
		if(fprintf(fp_config,"%s",tmp)!=strlen(tmp)) {
			perror("fprintf info");
			fclose(fp_config);
			return FALSE;
		}
		
		for(files=global_file->files;files->next != NULL;) {
			if(strlen(files->newname)!=0) {
				if(fprintf(fp_config,"%s ",files->newname)!=(strlen(files->newname)+1)) {
					perror("fprintf");
					fclose(fp_config);
					return FALSE;
				}
				strncpy(files->name,files->newname,FILE_NAME_LEN);
				memset(files->newname,'\0',FILE_NAME_LEN);
				
			} else {
				if(fprintf(fp_config,"%s ",files->name)!=(strlen(files->name)+1)) {
					perror("fprintf");
					fclose(fp_config);
					return FALSE;
				}
			}
			files=files->next;
		}
		fclose(fp_config);
		global_file->modify=FALSE;
	}
	return TRUE;
}
